#include <iostream>
using namespace std;
#include "function.h"
#include <vector>
#include <algorithm>

void administrators() // 管理员
{
    while (true)
    {
        int i = 0;
        cout << "请输入您想操作的功能编号" << endl;
        cout << "1.注册学生信息(用户名,密码)" << endl;
        cout << "2.录入图书信息" << endl;
        cout << "3.查询学生借阅状态" << endl;
        cout << "4.返回上一层" << endl;
        cout << "5.退出" << endl;
        cin >> i;
        if (i == 4)
        {
            break;
        }
        switch (i)
        {
        case 1:
        {

            student newStudent;
            cout << "请输入注册学生的信息用户名" << endl;
            cin >> newStudent.name;
            cout << "请输入注册学生的信息密码" << endl;
            cin >> newStudent.password;
            students.push_back(newStudent);
            cout << "学生注册成功！按任意键结束...\n";

            getchar();
            getchar();
            system("clear");
            break;
        }
        case 2:
        {

            book newBook;
            cout << "请输入录入书名" << endl;
            cin >> newBook.Booktitle;
            cout << "请输入作者" << endl;
            cin >> newBook.author;

            bool exists = false;
            for (const auto &b : books)
            {
                if (b.Booktitle == newBook.Booktitle && b.author == newBook.author)
                {
                    exists = true;
                    break;
                }
            }

            if (exists)
            {
                cout << "图书已存在，录入失败。" << endl;
            }
            else
            {
                books.push_back(newBook); // 录入图书信息
                cout << "图书录入成功！按任意键结束!" << endl;
                getchar();
                getchar();
                system("clear");
            }

            break;
        }
        case 3:
        {

            string tempName;
            cout << "请输入想要查询的学生姓名" << endl;
            cin >> tempName;
            auto it = find_if(students.begin(), students.end(), [&tempName](const student &s)
                              { return s.name == tempName; });

            if (it != students.end())
            {
                cout << "学生 " << tempName << " 的借阅记录：" << endl;
                if (it->borrowedBooks.empty())
                {
                    cout << "暂无借阅记录。" << endl;
                }
                else
                {
                    for (const auto &b : it->borrowedBooks)
                    {
                        cout << "书名: " << b.Booktitle << ", 作者: " << b.author << endl;
                    }
                }
            }
            else
            {
                cout << "未找到学生 " << tempName << " 的信息。" << endl;
            }
            break;
        }
        case 4:
        {
            break;
        }
        case 5:
        {
            return;
        }
        default:
            cout << "无效输入，请重新输入" << endl;
            break;
        }
    }
}

void studentMenu(const string &username) // 学生
{
    auto &currentStudent = *find_if(students.begin(), students.end(), [&username](const student &s)
                                    { return s.name == username; });

    while (1)
    {
        int i = 0;
        cout << "功能菜单:" << endl;
        cout << "1.借书(上限10本书)" << endl;
        cout << "2.借书记录" << endl;
        cout << "3.归还书籍" << endl;
        cout << "4.图书大厅" << endl;
        cout << "5.返回上一层" << endl;
        cout << "6.退出" << endl;
        cout << "请输入功能编号:" << endl;
        cin >> i;
        if (i == 5)
        {
            break;
        }

        switch (i)
        {
        case 1:
        {

            cout << "当前可借阅的图书列表" << endl;

            if (books.empty())
            {
                cout << "暂无图书可借阅。" << endl;
                cout << "按任意键结束!";
                getchar();
                getchar();
                system("clear");
                break;
            }

            int index = 1;
            for (const auto &b : books)
            {
                cout << index++ << ". 书名: " << b.Booktitle << ", 作者: " << b.author << endl;
            }

            cout << "请输入要借阅的图书编号: ";
            int bookIndex;
            cin >> bookIndex;

            if (bookIndex <= 0 || bookIndex > books.size())
            {
                cout << "无效编号。" << endl;
                break;
            }
            // 查找当前学生借阅数量

            if (currentStudent.borrowedBooks.size() >= 10)
            {
                cout << "借阅失败:超出借书上限(10本)" << endl;
            }
            else
            {
                currentStudent.borrowedBooks.push_back(books[bookIndex - 1]); // 更新学生借阅记录
                books.erase(books.begin() + bookIndex - 1);                   // 移除已借图书
                cout << "借书成功！" << endl;
                cout << "借书成功！按任意键结束!";
                getchar();
                getchar();
                system("clear");
            }
            break;
        }
        case 2:
        {

            cout << "您的借书记录为:" << endl;

            if (currentStudent.borrowedBooks.empty())
            {
                cout << "暂无借阅记录。" << endl;
            }
            else
            {
                int index = 1;
                for (const auto &b : currentStudent.borrowedBooks)
                {
                    cout << index++ << ". 书名: " << b.Booktitle << ", 作者: " << b.author << endl;
                }
                cout << endl;
                cout << "按任意键结束!";
                getchar();
                getchar();
                system("clear");
            }
            break;
        }
        case 3:
        {

            if (currentStudent.borrowedBooks.empty())
            {
                cout << "您没有借阅任何书籍。" << endl;
                break;
            }

            cout << "您的借阅记录：" << endl;
            int index = 1;
            for (const auto &b : currentStudent.borrowedBooks)
            {
                cout << index++ << ". 书名: " << b.Booktitle << ", 作者: " << b.author << endl;
            }

            cout << "请输入要归还的书籍编号: ";
            int returnIndex;
            cin >> returnIndex;

            if (returnIndex <= 0 || returnIndex > currentStudent.borrowedBooks.size())
            {
                cout << "无效编号。" << endl;
            }
            else
            {
                book returnedBook=currentStudent.borrowedBooks[returnIndex-1];//获取归还的图书信息
                currentStudent.borrowedBooks.erase(currentStudent.borrowedBooks.begin() + returnIndex - 1);//移除借阅记录
                books.posh_back(returnedBook);//将归还的图书添加回图书大厅
                cout << "图书归还成功！" << endl;
                cout << "按任意键结束!";
                getchar();
                getchar();
                system("clear");
            }
            break;
        }
        case 4:
        {

            cout << "图书大厅：" << endl;
            for (const auto &b : books)
            {
                cout << "书名: " << b.Booktitle << ", 作者: " << b.author << endl;
            }
            cout << "按任意键结束!";
            getchar();
            getchar();
            system("clear");
            break;
        }
        case 5:
        {
            break;
        }
        case 6:
        {
            return;
        }
        default:
            cout << "无效输入，请按任意键重新输入" << endl;
            getchar();
            getchar();
            system("clear");
        }
    }
}

// void wuchangyuan()
// {

// }
